package libs;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import entity.User;

import bookException.BookException;
import bookException.BookExceptionCode;



public class UserValidation {
	
	
	public static void validateUser(User user) throws BookException {	
		//Name
		ValidationMethods.isEmpty(user.getName(), new BookException(BookExceptionCode.EXCEPTION_NAME_EMPTY));
		ValidationMethods.isValid(user.getName(), new BookException(BookExceptionCode.EXCEPTION_NAME_MIN_LENGTH), Pattern.compile("^[a-z0-9_-]{2,}$"));
		ValidationMethods.isValid(user.getName(), new BookException(BookExceptionCode.EXCEPTION_NAME_MAX_LENGTH), Pattern.compile("^[a-z0-9_-]{0,20}$"));
		
		//Surname
		ValidationMethods.isEmpty(user.getSurname(), new BookException(BookExceptionCode.EXCEPTION_SURNAME_EMPTY));
		ValidationMethods.isValid(user.getSurname(), new BookException(BookExceptionCode.EXCEPTION_SURNAME_MIN_LENGTH), Pattern.compile("^[a-z0-9_-]{2,}$"));
		ValidationMethods.isValid(user.getSurname(), new BookException(BookExceptionCode.EXCEPTION_SURNAME_MAX_LENGTH), Pattern.compile("^[a-z0-9_-]{0,30}$"));
	    
		//Middlename
		ValidationMethods.isEmpty(user.getMiddlename(), new BookException(BookExceptionCode.EXCEPTION_MIDDLENAME_EMPTY));
		ValidationMethods.isValid(user.getMiddlename(), new BookException(BookExceptionCode.EXCEPTION_MIDDLENAME_MIN_LENGTH), Pattern.compile("^[a-z0-9_-]{2,}$"));
		ValidationMethods.isValid(user.getMiddlename(), new BookException(BookExceptionCode.EXCEPTION_MIDDLENAME_MAX_LENGTH), Pattern.compile("^[a-z0-9_-]{0,30}$"));
		
		//Mobile
		ValidationMethods.isEmpty(user.getMobile(), new BookException(BookExceptionCode.EXCEPTION_MOBILE_EMPTY));
		ValidationMethods.isValid(user.getMobile(), new BookException(BookExceptionCode.EXCEPTION_MOBILE_MIN_LENGTH), Pattern.compile("^[0-9]{6,}$"));
		ValidationMethods.isValid(user.getMobile(), new BookException(BookExceptionCode.EXCEPTION_MOBILE_MAX_LENGTH), Pattern.compile("^[0-9]{0,16}"));
		ValidationMethods.isValid(user.getMobile(), new BookException(BookExceptionCode.EXCEPTION_MOBILE_DIGITS), Pattern.compile("^[0-9]*$"));
		
		//Homephone
		if(!ValidationMethods.isEmpty(user.getPhonehome()))
		{
		//	ValidationMethods.isEmpty(user.getPhonehome(), new BookException(BookExceptionCode.EXCEPTION_HOMEPHONE_EMPTY));
			ValidationMethods.isValid(user.getPhonehome(), new BookException(BookExceptionCode.EXCEPTION_HOMEPHONE_MIN_LENGTH), Pattern.compile("^[0-9]{6,}$"));
			ValidationMethods.isValid(user.getPhonehome(), new BookException(BookExceptionCode.EXCEPTION_HOMEPHONE_MAX_LENGTH), Pattern.compile("^[0-9]{0,16}$"));
			ValidationMethods.isValid(user.getPhonehome(), new BookException(BookExceptionCode.EXCEPTION_HOMEPHONE_DIGITS), Pattern.compile("^[0-9]*$"));
		}
		
		//Address
		if(!ValidationMethods.isEmpty(user.getAddress())){
		//	ValidationMethods.isEmpty(user.getAddress(), new BookException(BookExceptionCode.EXCEPTION_ADDRESS_EMPTY));
			ValidationMethods.isValid(user.getAddress(), new BookException(BookExceptionCode.EXCEPTION_ADDRESS_MIN_LENGTH), Pattern.compile("^[a-z0-9_-]{4,}$"));
			ValidationMethods.isValid(user.getAddress(), new BookException(BookExceptionCode.EXCEPTION_ADDRESS_MAX_LENGTH), Pattern.compile("^[a-z0-9_-]{0,50}$"));
		}
		
		//Mail
		if(!ValidationMethods.isEmpty(user.getMail()))
		{
		//	ValidationMethods.isEmpty(user.getMail(), new BookException(BookExceptionCode.EXCEPTION_ADDRESS_EMPTY));
			ValidationMethods.isValid(user.getMail(), new BookException(BookExceptionCode.EXCEPTION_MAIL_MAX_LENGTH), Pattern.compile("^[a-z0-9_-]{0,50}$"));
			ValidationMethods.isValid(user.getMail(), new BookException(BookExceptionCode.EXCEPTION_MAIL_MIN_LENGTH), Pattern.compile("^[a-z0-9_-]{2,}$"));
		
			ValidationMethods.isValid(user.getMail(), new BookException(BookExceptionCode.EXCEPTION_MAIL_VALIDATION), Pattern.compile("[a-zA-Z]{1}[a-zA-Z\\d\\u002E\\u005F]+@([a-zA-Z]+\\u002E){1,2}((net)|(com)|(org))"));	
		}
	}
	
		public static boolean validateUserForm(User user){
			if(ValidationMethods.isEmpty(user.getName()) ||
				ValidationMethods.isEmpty(user.getSurname()) ||
				ValidationMethods.isEmpty(user.getMiddlename()) ||
				ValidationMethods.isEmpty(user.getMobile()) ||
				ValidationMethods.isEmpty(user.getMobile()) ||
				ValidationMethods.isEmpty(user.getPhonehome()) ||
				ValidationMethods.isEmpty(user.getAddress()) ||
				ValidationMethods.isEmpty(user.getMail())) 
				{
					return true;
				}
			return false;
		}

}